<template>
  <Banner />
  <Container padding-top="0" padding-bottom="0">
    <template v-if="newsCategory && newsData">
      <div class="news-content">
        <div class="content">
          <div class="left">
            <div class="header-box">
              <h1>{{ $t("content.news.category_title") }}</h1>
            </div>
            <div class="side-nav no-scrollbar">
              <div
                class="nav-item"
                :class="activeIndex == index ? 'active' : ''"
                v-for="(item, index) in newsCategory.data"
                :key="item.id"
                @click="onClickNewsCategoryItem(index)"
              >
                {{ item.title }}
              </div>
            </div>
          </div>
          <div class="right">
            <h1>{{ $t("content.news.news_title") }}</h1>
            <div class="news-list">
              <div
                class="news-item"
                @click="onClickNewsItem(item.id)"
                v-for="item in newsData.data"
                :key="item.id"
              >
                <div class="title">{{ item.title }}</div>
                <div class="time">{{ item.create_time.split(" ")[0] }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <div class="pc-skeleton"></div>
    </template>
  </Container>
</template>

<script setup lang="ts">
import { onBeforeMount, ref } from "vue";
import { useRoute } from "vue-router";
import {
  postNewsIndex,
  postNewsCategory,
  postNewsView,
} from "../../../api/news";

const route = useRoute();

const activeIndex = ref(0);

const onClickNewsCategoryItem = (index: number) => {
  if (activeIndex.value == index) return;
  activeIndex.value = index;
  getNewsData();
};

const newsCategory = ref();
const getNewsCategoryData = async () => {
  let res = await postNewsCategory();
  newsCategory.value = res.data;
  getNewsData();
};

const newsData = ref();
const getNewsData = async () => {
  let cat_id = newsCategory.value.data[activeIndex.value].id;
  let res = await postNewsIndex(cat_id);
  newsData.value = res.data;
};

const onClickNewsItem = async (id: number) => {
  const params = {
    ...route.query,
    id,
  };
  goto("/news/detail", params);
};

onBeforeMount(() => {
  getNewsCategoryData();
});
</script>
<style scoped lang="less">
.news-content {
  padding-top: 30px;
  h1 {
    font-size: 30px;
    margin-bottom: 25px;
    height: 35px;
    line-height: 55px;
  }
  .content {
    display: flex;
    .header-box {
      h1 {
        height: 55px;
        line-height: 55px;
        position: relative;
        font-size: 22px;
        margin-bottom: 10px;
        &::before {
          position: absolute;
          bottom: 0;
          left: 0;
          width: 37px;
          height: 3px;
          background: #16b6d2;
          content: "";
        }
      }
    }
  }
  .side-nav {
    width: 240px;
    font-size: 16px;

    .nav-item {
      height: 36px;
      line-height: 36px;
      color: #737373;
      &.active {
        color: #16b6d2;
      }
      &:hover {
        color: #16b6d2;
        cursor: pointer;
      }
    }
  }
  .right {
    flex: 1;
  }
  .news-list {
    font-size: 14px;
    .news-item {
      display: flex;
      justify-content: space-between;
      height: 48px;
      line-height: 48px;
      border-bottom: 1px solid #ddd;
      &:hover {
        color: #16b6d2;
        cursor: pointer;
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .news-content {
    padding-top: 82px;
    h1,
    .header-box {
      display: none;
    }
    .content {
      flex-direction: column;
      .left {
        .side-nav {
          display: flex;
          white-space: nowrap;
          width: 100%;
          font-size: 72px;
          overflow-x: auto;
          .nav-item {
            padding: 82px;
            height: auto;
            position: relative;
            &.active {
              &::before {
                content: "";
                position: absolute;
                bottom: 0;
                left: 50%;
                transform: translateX(-50%);
                width: 80%;
                height: 8px;
                background-color: #16b6d2;
                border-radius: 100px;
              }
            }
          }
        }
      }
      .right {
        .news-list {
          font-size: 64px;
          .news-item {
            height: 246px;
            line-height: 246px;
          }
        }
      }
    }
  }
}
</style>
